package com.btpj.hospital_server.service;

import com.btpj.hospital_server.dto.param.HospitalInfoParam;
import com.btpj.hospital_server.entity.HospitalInfo;
import com.btpj.hospital_server.entity.HospitalOutpatientRelation;
import com.btpj.hospital_server.entity.HospitalSpecial;
import com.btpj.hospital_server.entity.HospitalSpecialRelation;
import com.btpj.hospital_server.mapper.HospitalInfoMapper;
import com.btpj.hospital_server.mapper.HospitalOutpatientRelationMapper;
import com.btpj.hospital_server.mapper.HospitalSpecialMapper;
import com.btpj.hospital_server.mapper.HospitalSpecialRelationMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 医院信息Service
 *
 * @author BTPJ  2020/8/24
 */
@Service
public class HospitalInfoService {

    @Resource
    private HospitalInfoMapper hospitalInfoMapper;

    @Resource
    private HospitalSpecialRelationMapper hospitalSpecialRelationMapper;

    @Resource
    private HospitalOutpatientRelationMapper hospitalOutpatientRelationMapper;

    @Resource
    private HospitalSpecialMapper hospitalSpecialMapper;

    /**
     * 获取医院信息列表
     *
     * @param name 医院名称（模糊匹配）
     * @return 医院信息分页列表
     */
    public List<HospitalInfo> getList(String name) {
        return hospitalInfoMapper.selectAll(name);
    }

    /**
     * 是否存在指定ID的医院
     *
     * @param id 医院Id
     * @return 是否存在指定ID的医院
     */
    public boolean existHospital(Long id) {
        return hospitalInfoMapper.selectByPrimaryKey(id) != null;
    }

    /**
     * 是否存在相同名称的医院
     *
     * @param name 医院名称
     * @return 是否存在相同名称的医院
     */
    public boolean existHospital(String name) {
        return hospitalInfoMapper.selectByName(name) > 0;
    }

    /**
     * 添加医院信息
     *
     * @param param HospitalInfoParam
     * @return 是否添加成功
     */
    public boolean addHospitalInfo(HospitalInfoParam param) {
        HospitalInfo hospitalInfo = new HospitalInfo();
        BeanUtils.copyProperties(param, hospitalInfo);
        hospitalInfo.setGmtCreate(new Date());
        hospitalInfo.setGmtModified(new Date());
        return hospitalInfoMapper.insert(hospitalInfo) > 0;
    }

    /**
     * 添加医院信息
     *
     * @param id    医院ID
     * @param param HospitalInfoParam
     * @return 是否添加成功
     */
    public boolean updateHospitalInfo(Long id, HospitalInfoParam param) {
        HospitalInfo hospitalInfo = hospitalInfoMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(param, hospitalInfo);
        hospitalInfo.setGmtModified(new Date());
        return hospitalInfoMapper.updateByPrimaryKeySelective(hospitalInfo) > 0;
    }

    /**
     * 获取医院信息详情
     *
     * @param id 医院ID
     * @return 医院信息
     */
    public HospitalInfo getHospitalInfo(Long id) {
        return hospitalInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 删除医院信息
     *
     * @param id 医院ID
     * @return 是否删除成功
     */
    public boolean deleteHospitalInfo(Long id) {
        return hospitalInfoMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 指定ID的医院中是否包含了指定ID的专科
     *
     * @param hospitalId 医院ID
     * @param specialId  专科ID
     * @return 指定ID的医院中是否包含了指定ID的专科
     */
    public boolean existSpecialRelation(Long hospitalId, Long specialId) {
        return hospitalSpecialRelationMapper.selectByHospitalIdAndSpecialId(hospitalId, specialId) > 0;
    }

    /**
     * 指定ID的医院中是否包含了指定ID的门诊
     *
     * @param hospitalId   医院ID
     * @param outpatientId 门诊ID
     * @return 指定ID的医院中是否包含了指定ID的门诊
     */
    public boolean existOutpatientRelation(Long hospitalId, Long outpatientId) {
        return hospitalOutpatientRelationMapper.selectByHospitalIdAndOutpatientId(hospitalId, outpatientId) > 0;
    }

    /**
     * 向指定ID的医院中添加指定ID的专科
     *
     * @param hospitalId 医院ID
     * @param specialId  专科ID
     * @return 是否添加成功
     */
    public boolean addSpecialRelation(Long hospitalId, Long specialId) {
        HospitalSpecialRelation hospitalSpecialRelation = new HospitalSpecialRelation();
        hospitalSpecialRelation.setHospitalId(hospitalId);
        hospitalSpecialRelation.setSpecialId(specialId);
        hospitalSpecialRelation.setGmtCreate(new Date());
        hospitalSpecialRelation.setGmtModified(new Date());
        return hospitalSpecialRelationMapper.insertSelective(hospitalSpecialRelation) > 0;
    }

    /**
     * 向指定ID的医院中添加指定ID的门诊
     *
     * @param hospitalId   医院ID
     * @param outpatientId 门诊ID
     * @return 是否添加成功
     */
    public boolean addOutpatientRelation(Long hospitalId, Long outpatientId) {
        HospitalOutpatientRelation hospitalOutpatientRelation = new HospitalOutpatientRelation();
        hospitalOutpatientRelation.setHospitalId(hospitalId);
        hospitalOutpatientRelation.setOutpatientId(outpatientId);
        hospitalOutpatientRelation.setGmtCreate(new Date());
        hospitalOutpatientRelation.setGmtModified(new Date());
        return hospitalOutpatientRelationMapper.insertSelective(hospitalOutpatientRelation) > 0;
    }

    /**
     * 向指定ID的医院中移除指定ID的专科
     *
     * @param hospitalId 医院ID
     * @param specialId  专科ID
     * @return 是否删除成功
     */
    public boolean deleteSpecialRelation(Long hospitalId, Long specialId) {
        return hospitalSpecialRelationMapper.deleteByHospitalIdAndSpecialId(hospitalId, specialId) > 0;
    }

    /**
     * 向指定ID的医院中删除除指定ID的门诊
     *
     * @param hospitalId   医院ID
     * @param outpatientId 门诊ID
     * @return 是否删除成功
     */
    public boolean deleteOutpatientRelation(Long hospitalId, Long outpatientId) {
        return hospitalOutpatientRelationMapper.deleteByHospitalIdAndOutpatientId(hospitalId, outpatientId) > 0;
    }

    /**
     * 获取医院的所有专科列表
     *
     * @param hospitalId 医院ID
     * @return 医院的所有专科列表
     */
    public List<HospitalSpecial> getSpecialList(Long hospitalId) {
        List<HospitalSpecialRelation> hospitalSpecialRelations = hospitalSpecialRelationMapper.selectByHospitalId(hospitalId);
        List<HospitalSpecial> hospitalSpecials = new ArrayList<>();

        if (hospitalSpecialRelations != null) {
            for (HospitalSpecialRelation hospitalSpecialRelation : hospitalSpecialRelations) {
                HospitalSpecial hospitalSpecial = hospitalSpecialMapper.selectByPrimaryKey(hospitalSpecialRelation.getSpecialId());
                hospitalSpecials.add(hospitalSpecial);
            }
        }

        return hospitalSpecials;
    }

    /**
     * 获取指定医院所包含的门诊ID
     *
     * @param hospitalId 医院ID
     * @return 指定医院所包含的门诊ID
     */
    public List<Long> getOutpatientIdListByHospitalId(Long hospitalId) {
        return hospitalOutpatientRelationMapper.selectByHospitalId(hospitalId);
    }
}
